package com.haixiaoke.saas.manage.messageCenter.controller;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.messageCenter.domain.OrdinaryMessage;
import com.haixiaoke.saas.messageCenter.service.IOrdinaryMessageService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 普通消息推送Controller
 *
 * @author panda
 * @date 2024-11-08
 */
@RestController
@RequestMapping("/manage/messageCenter/ordinaryMessage")
public class ManageOrdinaryMessageController extends BaseController {
    @Autowired
    private IOrdinaryMessageService ordinaryMessageService;

    /**
     * 查询普通消息推送列表
     */
    @PreAuthorize("@ss.hasPermi('manage:ordinaryMessage:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrdinaryMessage ordinaryMessage) {
        startPage();
        List<OrdinaryMessage> list = ordinaryMessageService.selectOrdinaryMessageList(ordinaryMessage);
        return getDataTable(list);
    }

    /**
     * 获取普通消息推送详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:ordinaryMessage:query')")
    @GetMapping(value = "/{ordinaryMessageId}")
    public AjaxResult getInfo(@PathVariable("ordinaryMessageId") Long ordinaryMessageId) {
        return success(ordinaryMessageService.selectOrdinaryMessageByOrdinaryMessageId(ordinaryMessageId));
    }

    /**
     * 新增普通消息推送
     */
    @PreAuthorize("@ss.hasPermi('manage:ordinaryMessage:add')")
    @Log(title = "普通消息推送", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody OrdinaryMessage ordinaryMessage) {
        if (StrUtil.isBlank(ordinaryMessage.getTenantId()) || StrUtil.isBlank(ordinaryMessage.getTenantName())) {
            return error("租户信息错误");
        }

        ordinaryMessage.setCreateBy(getUsername());
        return ordinaryMessageService.insertOrdinaryMessage(ordinaryMessage);
    }

    /**
     * 修改普通消息推送
     */
    @PreAuthorize("@ss.hasPermi('manage:ordinaryMessage:edit')")
    @Log(title = "普通消息推送", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody OrdinaryMessage ordinaryMessage) {
        ordinaryMessage.setUpdateBy(getUsername());
        return ordinaryMessageService.updateOrdinaryMessage(ordinaryMessage);
    }

    /**
     * 删除普通消息推送
     */
    @PreAuthorize("@ss.hasPermi('manage:ordinaryMessage:remove')")
    @Log(title = "普通消息推送", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ordinaryMessageId}")
    public AjaxResult remove(@PathVariable("ordinaryMessageId") Long ordinaryMessageId) {
        return toAjax(ordinaryMessageService.deleteOrdinaryMessageByOrdinaryMessageId(ordinaryMessageId));
    }
}
